﻿using Common.FrontEnd;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Util.Props;

namespace FrontEnd.EndPoint
{
    /// <summary>
    /// Given a sequence of Data, filters out the non-speech regions. The sequence of Data should have the speech and
    /// non-speech regions marked out by the SpeechStartSignal and SpeechEndSignal, using the {@link SpeechMarker
    /// SpeechMarker}.
    /// <p/>
    /// Only one speech region</b> <p>In the first case, the data stream has only one speech region: <p><img
    /// src="doc-files/one-region.gif"> <br><i>Figure 1: A data stream with only one speech region</i>. <p>After filtering,
    /// the non-speech regions are removed, and becomes: <p><img src="doc-files/one-region-filtered.gif"> <br><i>Figure 2: A
    /// data stream with only on speech region after filtering.</i>
    /// </summary>
    public class NonSpeechDataFilter: BaseDataProcessor
    {
        private Boolean _inSpeech;

        public NonSpeechDataFilter() 
        {
        }

        override
        public void newProperties(PropertySheet ps)
        {
            base.newProperties(ps);
        }


        /** Initializes this data processor */
        public void initialize() {
            base.initialize();

            this._inSpeech = false;
        }


        public IData getData()
        {
            IData data = readData();

            while (data != null
                    && !(data is DataEndSignal) && !(data is DataStartSignal)
                    && !(data is SpeechEndSignal) && !_inSpeech) {
                data = readData();
            }

            return data;
        }


        private IData readData() 
        {
            IData data = getPredecessor().getData();

            if (data is SpeechStartSignal)
                _inSpeech = true;

            if (data is SpeechEndSignal)
                _inSpeech = false;

            return data;
        }
    }
}
